package com.mybatisplus.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatisplus.demo.mapper.UserMapper;
import com.mybatisplus.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 项目名称：mybatisplus_demo_1
 * 类 名 称：UserController
 * 类 描 述：TODO
 * 创建时间：2020/5/18
 * 创 建 人：Areli
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserMapper userMapper;

    @PutMapping("/update2")
    public Integer update2(){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name","李艺伟").eq("age",28);

        User user = new User();
        user.setEmail("update2@email");
        int rows = userMapper.update(user, updateWrapper);
        return rows;
    }

    //更新用户信息
    @PutMapping("/update1")
    public Integer update1(){
        User user = new User();
        user.setId(1088250446457389058L);
        user.setEmail("update@email");
        int rows = userMapper.updateById(user);
        return rows;
    }

    //查询年龄大于20 的用户信息，并以每页容量为两条分页的形式返回
    @GetMapping("/selectPage")
    public IPage<User> selectPage(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("age",20);
        //设置当前页和页容量
        Page<User> page = new Page<>(1,2);
        IPage<User> userIPage = userMapper.selectPage(page,queryWrapper);
        System.out.println("总页数: "+userIPage.getPages());
        System.out.println("总记录数: "+userIPage.getTotal());
        return userIPage;
    }

    //查找为王姓的员工的姓名和年龄
    @GetMapping("/selectByWrapper7")
    public List<User> selectByWrapper7(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name","age").likeRight("name","王");
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

    //年龄为30,31,35,34的员工
    @GetMapping("/selectByWrapper6")
    public List<User> selectByWrapper6(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("age", Arrays.asList(30,31,34,35)).last("limit 1");
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

    //（年龄小于40或者邮箱不为空）并且名字为王姓
    @GetMapping("/selectByWrapper5")
    public List<User> selectByWrapper5(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.nested(wq->wq.lt("age",40).or().isNotNull("email")).likeRight("name","王");
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

    //名字为王姓，（年龄小于40，并且年龄大于20，并且邮箱不为空）
    @GetMapping("/selectByWrapper4")
    public List<User> selectByWrapper4(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("name","王").and(wq->wq.between("age",20,40).and(wqq->wqq.isNotNull("email")));
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

    //名字为王姓，（年龄小于40或者邮箱不为空）
    @GetMapping("/selectByWrapper3")
    public List<User> selectByWrapper3(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("name","王").and(wq->wq.lt("age",40).or().isNotNull("email"));
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

    //创建日期为2019年2月14日并且直属上级姓名为王姓
    @GetMapping("/selectByWrapper2")
    public List<User> selectByWrapper2(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}","2019-02-14")
                .inSql("manager_id","select id from user where name like '王%'");
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

    //查询名字中包含{name} 并且年龄小于{age}
    @GetMapping("/selectByWrapper")
    public List<User> selectByWrapper(String name,Integer age){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name).lt("age",age);
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

    @PostMapping
    public Integer insert(){
        User user = new User();
        user.setAge(31);
        user.setManagerId("1088250446457389058");
        user.setCreateTime(new Date());
        int insert = userMapper.insert(user);
        return insert;
    }

    @GetMapping("/all")
    public List<User> select(){
        List<User> users = userMapper.selectList(null);
        return users;
    }
}
